/*
 * TestDumpCPPMacroInfo.c
 *
 *  Created on: 2012/05/31
 *      Author: guanxiaoying
 */

#include "TestDump.h"

static VOID dumpMacroInfo( AST astMacro ){

	sprintf( tempBuf, header_format, dumpGetStr(XML_RECORD), astMacro );
	dumpOutputStream( tempBuf );


	sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_DIRECTIVE), "",
			CMacroInfo.getDirectiveInfo( astMacro ));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_DIRECTIVE)  );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_STRINGVALUE), "");
	dumpOutputStream( tempBuf );
	dumpParameterStr( CMacroInfo.getMacroName( astMacro ) );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_STRINGVALUE)  );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_REPLSTTKN), "first",
			CMacroInfo.getFirstToken( astMacro ));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_REPLSTTKN)  );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_REPLSTTKN), "last",
			CMacroInfo.getLastToken( astMacro ));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_REPLSTTKN)  );
	dumpOutputStream( tempBuf );

	if( CMacroInfo.isFunctionMacro( astMacro ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "");
		dumpOutputStream( tempBuf );
		dumpParameterStr( "function-like" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND)  );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_VALUE), "parameters-number");
		dumpOutputStream( tempBuf );
		if( CMacroInfo.hasVariableParameters( astMacro ) ){
			sprintf( constantBuffer, "%d%s", CMacroInfo.getParameterNum( astMacro ) - 1, "+" );
		}else{
			sprintf( constantBuffer, "%d", CMacroInfo.getParameterNum( astMacro ));
		}
		dumpParameterStr( constantBuffer );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_VALUE)  );
		dumpOutputStream( tempBuf );

	}else{
		sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_KIND), "");
		dumpOutputStream( tempBuf );
		dumpParameterStr( "object-like" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_KIND)  );
		dumpOutputStream( tempBuf );
	}
	sprintf( tempBuf, sub_value_header_format, dumpGetStr(XML_FLAG), "");
	dumpOutputStream( tempBuf );
	dumpParameterStr( CMacroInfo.isValid( astMacro )?"true":"false" );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, tail_format, dumpGetStr(XML_RECORD) );
	dumpOutputStream( tempBuf );
}
VOID DumpMacroInfo( PSZ szPath, PSZ postfix ){
	AST astMacro;
	AST astMax = CPPOutput.CMacroInfo->getMax();
	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS == dumpOutputStreamInit_file(file_buf)){
		sprintf( tempBuf, top_header, dumpGetStr(XML_MACROINFO) );
		dumpOutputStream( tempBuf );
		for( astMacro = MIN_ARRAY; astMacro <= astMax; astMacro++ ){
			dumpMacroInfo( astMacro );
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_MACROINFO) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}

